package com.czl.module_service.fragment

import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.HealthReportBean
import com.czl.base.data.bean.tengyun.NoticeListBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.activity.HealthReportDetailActivity
import com.czl.module_service.activity.NoticeDetailActivity
import com.czl.module_service.adapter.HealthReportAdapter
import com.czl.module_service.adapter.NoticeListAdapter
import com.czl.module_service.databinding.ActivityHealthReportListBinding
import com.czl.module_service.databinding.ActivityNoticeListBinding
import com.czl.module_service.viewmodel.HealthReportListViewModel
import com.czl.module_service.viewmodel.NoticeListViewModel

/**
 * 疫情公告列表
 */
@Route(path = AppConstants.Router.Service.F_HEALTH_REPORT_LIST)
class HealthReportListFragment : BaseFragment<ActivityHealthReportListBinding, HealthReportListViewModel>() {
    private lateinit var mAdapter: HealthReportAdapter

    override fun initContentView(): Int {
        return R.layout.activity_health_report_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
//        return 0
    }

    override fun initData() {
        viewModel.tvTitle.set("健康上报")
        initAdapter()
        reload()
    }

    private fun initAdapter(){
        mAdapter = HealthReportAdapter(R.layout.item_health_report).apply {
            setOnItemClickListener() { adapter, _, position ->
                val bean: HealthReportBean.Data = adapter.data[position] as HealthReportBean.Data
                val intent = Intent(context, HealthReportDetailActivity::class.java)
                intent.putExtra("data", bean.healthyId)
                context?.startActivity(intent)
            }
        }
        binding.ryCommon.apply{
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        var list = mutableListOf<Any>()
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            if (data != null && data.isFirstPage) {
                list.clear()
            }
            if (data?.list != null) {
                list = data.list as MutableList<Any>
            }
            handleRecyclerviewData(
                data == null,
                list,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.isLastPage
            )
        })
        LiveBusCenter.observeAddHealthyCompleteEvent(this) {
            initAdapter()
            reload()
        }
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}